using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace JGSY.CMS.LowCode.Platform.Application.Interfaces
{
    /// <summary>
    /// 云存储服务接口
    /// 支持多种云存储提供商的文件上传、下载、删除等操作
    /// </summary>
    public interface ICloudStorageService
    {
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="file">文件流</param>
        /// <param name="fileName">文件名</param>
        /// <param name="folder">文件夹路径</param>
        /// <param name="contentType">内容类型</param>
        /// <returns>文件访问URL</returns>
        Task<string> UploadAsync(Stream file, string fileName, string? folder = null, string? contentType = null);

        /// <summary>
        /// 上传文件（从IFormFile）
        /// </summary>
        /// <param name="file">表单文件</param>
        /// <param name="folder">文件夹路径</param>
        /// <returns>文件访问URL</returns>
        Task<string> UploadAsync(IFormFile file, string? folder = null);

        /// <summary>
        /// 批量上传文件
        /// </summary>
        /// <param name="files">文件列表</param>
        /// <param name="folder">文件夹路径</param>
        /// <returns>文件访问URL列表</returns>
        Task<List<string>> UploadBatchAsync(IEnumerable<IFormFile> files, string? folder = null);

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="fileUrl">文件URL</param>
        /// <returns>文件流</returns>
        Task<Stream> DownloadAsync(string fileUrl);

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="fileUrl">文件URL</param>
        /// <returns>是否成功</returns>
        Task<bool> DeleteAsync(string fileUrl);

        /// <summary>
        /// 批量删除文件
        /// </summary>
        /// <param name="fileUrls">文件URL列表</param>
        /// <returns>删除成功的数量</returns>
        Task<int> DeleteBatchAsync(IEnumerable<string> fileUrls);

        /// <summary>
        /// 检查文件是否存在
        /// </summary>
        /// <param name="fileUrl">文件URL</param>
        /// <returns>是否存在</returns>
        Task<bool> ExistsAsync(string fileUrl);

        /// <summary>
        /// 获取文件信息
        /// </summary>
        /// <param name="fileUrl">文件URL</param>
        /// <returns>文件信息</returns>
        Task<CloudFileInfo?> GetFileInfoAsync(string fileUrl);

        /// <summary>
        /// 生成预签名上传URL（用于前端直传）
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="folder">文件夹路径</param>
        /// <param name="expiration">过期时间</param>
        /// <returns>预签名URL信息</returns>
        Task<PreSignedUploadInfo> GeneratePreSignedUploadUrlAsync(string fileName, string? folder = null, TimeSpan? expiration = null);

        /// <summary>
        /// 生成预签名下载URL
        /// </summary>
        /// <param name="fileUrl">文件URL</param>
        /// <param name="expiration">过期时间</param>
        /// <returns>预签名下载URL</returns>
        Task<string> GeneratePreSignedDownloadUrlAsync(string fileUrl, TimeSpan? expiration = null);

        /// <summary>
        /// 列出指定文件夹下的文件
        /// </summary>
        /// <param name="folder">文件夹路径</param>
        /// <param name="maxResults">最大结果数</param>
        /// <returns>文件信息列表</returns>
        Task<List<CloudFileInfo>> ListFilesAsync(string? folder = null, int maxResults = 100);

        /// <summary>
        /// 复制文件
        /// </summary>
        /// <param name="sourceUrl">源文件URL</param>
        /// <param name="destinationPath">目标路径</param>
        /// <returns>新文件URL</returns>
        Task<string> CopyAsync(string sourceUrl, string destinationPath);

        /// <summary>
        /// 移动文件
        /// </summary>
        /// <param name="sourceUrl">源文件URL</param>
        /// <param name="destinationPath">目标路径</param>
        /// <returns>新文件URL</returns>
        Task<string> MoveAsync(string sourceUrl, string destinationPath);

        /// <summary>
        /// 获取存储统计信息
        /// </summary>
        /// <param name="folder">文件夹路径</param>
        /// <returns>存储统计</returns>
        Task<StorageStatistics> GetStatisticsAsync(string? folder = null);
    }

    /// <summary>
    /// 云文件信息
    /// </summary>
    public class CloudFileInfo
    {
        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName { get; set; } = string.Empty;

        /// <summary>
        /// 文件路径
        /// </summary>
        public string FilePath { get; set; } = string.Empty;

        /// <summary>
        /// 文件URL
        /// </summary>
        public string Url { get; set; } = string.Empty;

        /// <summary>
        /// 文件大小（字节）
        /// </summary>
        public long Size { get; set; }

        /// <summary>
        /// 内容类型
        /// </summary>
        public string? ContentType { get; set; }

        /// <summary>
        /// ETag
        /// </summary>
        public string? ETag { get; set; }

        /// <summary>
        /// 最后修改时间
        /// </summary>
        public DateTime LastModified { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedAt { get; set; }

        /// <summary>
        /// 元数据
        /// </summary>
        public Dictionary<string, string> Metadata { get; set; } = new();
    }

    /// <summary>
    /// 预签名上传信息
    /// </summary>
    public class PreSignedUploadInfo
    {
        /// <summary>
        /// 上传URL
        /// </summary>
        public string UploadUrl { get; set; } = string.Empty;

        /// <summary>
        /// 表单字段（POST上传时需要）
        /// </summary>
        public Dictionary<string, string> FormFields { get; set; } = new();

        /// <summary>
        /// 最终文件访问URL
        /// </summary>
        public string FileUrl { get; set; } = string.Empty;

        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime ExpiresAt { get; set; }
    }

    /// <summary>
    /// 存储统计信息
    /// </summary>
    public class StorageStatistics
    {
        /// <summary>
        /// 文件总数
        /// </summary>
        public int TotalFiles { get; set; }

        /// <summary>
        /// 总大小（字节）
        /// </summary>
        public long TotalSize { get; set; }

        /// <summary>
        /// 按文件类型分组统计
        /// </summary>
        public Dictionary<string, FileTypeStats> FileTypeStats { get; set; } = new();

        /// <summary>
        /// 按文件夹分组统计
        /// </summary>
        public Dictionary<string, FolderStats> FolderStats { get; set; } = new();
    }

    /// <summary>
    /// 文件类型统计
    /// </summary>
    public class FileTypeStats
    {
        /// <summary>
        /// 文件数量
        /// </summary>
        public int Count { get; set; }

        /// <summary>
        /// 总大小
        /// </summary>
        public long TotalSize { get; set; }
    }

    /// <summary>
    /// 文件夹统计
    /// </summary>
    public class FolderStats
    {
        /// <summary>
        /// 文件数量
        /// </summary>
        public int FileCount { get; set; }

        /// <summary>
        /// 子文件夹数量
        /// </summary>
        public int SubFolderCount { get; set; }

        /// <summary>
        /// 总大小
        /// </summary>
        public long TotalSize { get; set; }
    }
}
